#include <iostream>
#include <vector>
using namespace std;

class Solution {
	int max_profit(vector<int> &prices)
	{
		int n = prices.size();
		vector<pair<int, int>> dp(n, make_pair(0, 0));
		dp[0].first = -prices[0];
		dp[0].second = 0;
		for (int i = 1; i < n; i++) {
			dp[i].first = max(dp[i - 1].first, dp[i - 1].second - prices[i]);
			dp[i].second = max(dp[i - 1].first + prices[i], dp[i - 1].second);
		}
		return dp[n - 1].second;
	}

        int max_profit_it(vector<int> &prices)
        {
                int n = prices.size();
                int max_profit = 0;
                for(int i = 1; i < n; i++){
                        max_profit += max(0, prices[i] - prices[i - 1]);
                }
                return max_profit;
        }

    public:
	int maxProfit(vector<int> &prices)
	{
		return max_profit_it(prices);
	}
};

int main()
{
	vector<int> prices{ 7, 1, 5, 3, 6, 4 };
	cout << Solution().maxProfit(prices) << endl;
	return 0;
}